package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.entity.MerchantRole;
import com.ayf.payment.game.api.entity.MerchantRolePermission;
import com.ayf.payment.game.api.mapper.MerchantRolePermissionMapper;
import com.ayf.payment.game.api.service.MerchantRolePermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MerchantRolePermissionServiceImpl implements MerchantRolePermissionService {

    @Resource
    private MerchantRolePermissionMapper merchantRolePermissionMapper;

    @Override
    public boolean updateRolePermission(MerchantRole merchantRole) {
        MerchantRolePermission merchantRolePermission = new MerchantRolePermission();
        merchantRolePermission.setRoleId(merchantRole.getId());
        merchantRolePermissionMapper.delete(merchantRolePermission);
        return merchantRolePermissionMapper.addMerchantRolePermission(merchantRole.getId(),
                merchantRole.getPermissionIds()) > 0;
    }

    @Override
    public List<Integer> list(Integer roleId) {
        MerchantRolePermission merchantRolePermission=new MerchantRolePermission();
        merchantRolePermission.setRoleId(roleId);
        List<MerchantRolePermission> list = merchantRolePermissionMapper.select(merchantRolePermission);
        List<Integer> ids=new ArrayList<>();
        for (MerchantRolePermission permission : list) {
            ids.add(permission.getPermissionId());
        }
        return ids;
    }
}
